package com.xdtech.play.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.common.utils.EmptyUtil;
import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.play.dao.PlayMemberDao;
import com.xdtech.play.model.PlayMember;
import com.xdtech.play.service.PlayMemberService;
import com.xdtech.play.vo.PlayMemberItem;
import com.xdtech.sys.model.User;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2016-05-08 23:42:50
 * @since 1.0
 * @see
 */
@Service
public class PlayMemberServiceImpl implements PlayMemberService {
	private Log log = LogFactory.getLog(PlayMemberServiceImpl.class);
	@Autowired
	private PlayMemberDao playMemberDao;
	@Autowired
	private BaseDao baseDao;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:50
	 * @modified by
	 * @param entity
	 */
	public void save(PlayMember entity) {
		playMemberDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:50
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<PlayMember> entities) {
		playMemberDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:50
	 * @modified by
	 * @param entity
	 */
	public void delete(PlayMember entity) {
		playMemberDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:50
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		playMemberDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:50
	 * @modified by
	 * @param id
	 * @return
	 */
	public PlayMember get(Long id) {
		return playMemberDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:50
	 * @modified by
	 * @return
	 */
	public List<PlayMember> getAll() {
		return playMemberDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:50
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> playMembers = null;
		long rows = 0;
		if (pg!=null) {
			Page<PlayMember> page = playMemberDao.findPage(pg,"from PlayMember order by createTime desc", values);
			playMembers = BeanUtils.copyListProperties(
					PlayMemberItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<PlayMember> playMemberList = playMemberDao.find("from PlayMember order by id desc", values);
			playMembers = BeanUtils.copyListProperties(
					PlayMemberItem.class, playMemberList);
			rows = playMembers.size();
		}
		results.put("total", rows);
		results.put("rows", playMembers);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:50
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdatePlayMember(PlayMemberItem item) {
		PlayMember playMember = null;
		if (item.getId()==null) {
			playMember = new PlayMember();
		}else {
			playMember = playMemberDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(playMember, item);
		playMemberDao.save(playMember);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:50
	 * @modified by
	 * @param playMemberId
	 * @return
	 */
	public PlayMemberItem loadPlayMemberItem(Long playMemberId) {
		PlayMember playMember = playMemberDao.get(playMemberId);
		return PlayMemberItem.createItem(playMember);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:50
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deletePlayMemberInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:50
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deletePlayMemberInfo(List<Long> playMemberIds) {
		for (Long id : playMemberIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-08 23:42:50
	 * @modified by
	 * @return
	 */
	@Override
	public List<PlayMemberItem> loadItems() {
		List<PlayMember> playMembers = getAll();
		return PlayMemberItem.createItems(playMembers);
	}
	/* (非 Javadoc)
	 * checkNameIsExist
	 * <p>对override方法的补充说明，若不需要则删除此行</p>
	 * 
	 * @param valueOf
	 * @param name
	 * @return
	 * @see com.xdtech.play.service.PlayMemberService#checkNameIsExist(java.lang.Long, java.lang.String)
	 */
	@Override
	public boolean checkNameIsExist(Long memberId, String name)
	{
		List<PlayMember> members = playMemberDao.findByHql("from PlayMember where name=? and id!=?", name,memberId);
		return EmptyUtil.isNotEmpty(members);
	}

}
